// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.ai.states;

import java.util.HashMap;
import zombie.ai.State;
import zombie.characters.IsoGameCharacter;
import zombie.characters.IsoPlayer;
import zombie.characters.skills.PerkFactory;
import zombie.core.Core;
import zombie.core.Rand;
import zombie.core.skinnedmodel.advancedanimation.AnimEvent;
import zombie.debug.DebugOptions;
import zombie.iso.IsoDirections;
import zombie.iso.SpriteDetails.IsoFlagType;
import zombie.iso.objects.IsoWindow;

public final class OpenWindowState extends State {
    private static final OpenWindowState _instance = new OpenWindowState();
    private static final Integer PARAM_WINDOW = 1;

    public static OpenWindowState instance() {
        return _instance;
    }

    @Override
    public void enter(IsoGameCharacter owner) {
        owner.setIgnoreMovement(true);
        owner.setHideWeaponModel(true);
        HashMap hashMap = owner.getStateMachineParams(this);
        IsoWindow isoWindow = (IsoWindow)hashMap.get(PARAM_WINDOW);
        if (Core.bDebug
            && DebugOptions.instance.CheatWindowUnlock.getValue()
            && isoWindow.getSprite() != null
            && !isoWindow.getSprite().getProperties().Is("WindowLocked")) {
            isoWindow.Locked = false;
            isoWindow.PermaLocked = false;
        }

        if (isoWindow.north) {
            if ((float)isoWindow.getSquare().getY() < owner.getY()) {
                owner.setDir(IsoDirections.N);
            } else {
                owner.setDir(IsoDirections.S);
            }
        } else if ((float)isoWindow.getSquare().getX() < owner.getX()) {
            owner.setDir(IsoDirections.W);
        } else {
            owner.setDir(IsoDirections.E);
        }

        owner.setVariable("bOpenWindow", true);
    }

    @Override
    public void execute(IsoGameCharacter owner) {
        HashMap hashMap = owner.getStateMachineParams(this);
        if (owner.getVariableBoolean("bOpenWindow")) {
            IsoPlayer isoPlayer = (IsoPlayer)owner;
            if (!isoPlayer.pressedMovement(false) && !isoPlayer.pressedCancelAction()) {
                IsoWindow isoWindow = (IsoWindow)hashMap.get(PARAM_WINDOW);
                if (isoWindow == null || isoWindow.getObjectIndex() == -1) {
                    owner.setVariable("bOpenWindow", false);
                } else if (IsoPlayer.getInstance().ContextPanic > 5.0F) {
                    IsoPlayer.getInstance().ContextPanic = 0.0F;
                    owner.setVariable("bOpenWindow", false);
                    owner.smashWindow(isoWindow);
                    owner.getStateMachineParams(SmashWindowState.instance()).put(3, Boolean.TRUE);
                } else {
                    isoPlayer.setCollidable(true);
                    isoPlayer.updateLOS();
                    if (isoWindow.north) {
                        if ((float)isoWindow.getSquare().getY() < owner.getY()) {
                            owner.setDir(IsoDirections.N);
                        } else {
                            owner.setDir(IsoDirections.S);
                        }
                    } else if ((float)isoWindow.getSquare().getX() < owner.getX()) {
                        owner.setDir(IsoDirections.W);
                    } else {
                        owner.setDir(IsoDirections.E);
                    }

                    if (Core.bTutorial) {
                        if (owner.x != isoWindow.getX() + 0.5F && isoWindow.north) {
                            this.slideX(owner, isoWindow.getX() + 0.5F);
                        }

                        if (owner.y != isoWindow.getY() + 0.5F && !isoWindow.north) {
                            this.slideY(owner, isoWindow.getY() + 0.5F);
                        }
                    }
                }
            } else {
                owner.setVariable("bOpenWindow", false);
            }
        }
    }

    @Override
    public void exit(IsoGameCharacter owner) {
        owner.setIgnoreMovement(false);
        owner.clearVariable("bOpenWindow");
        owner.clearVariable("OpenWindowOutcome");
        owner.clearVariable("StopAfterAnimLooped");
        owner.setHideWeaponModel(false);
    }

    @Override
    public void animEvent(IsoGameCharacter owner, AnimEvent event) {
        HashMap hashMap = owner.getStateMachineParams(this);
        if (owner.getVariableBoolean("bOpenWindow")) {
            IsoWindow isoWindow = (IsoWindow)hashMap.get(PARAM_WINDOW);
            if (isoWindow == null) {
                owner.setVariable("bOpenWindow", false);
            } else {
                if (event.m_EventName.equalsIgnoreCase("WindowAnimLooped")) {
                    if ("start".equalsIgnoreCase(event.m_ParameterValue)) {
                        if (isoWindow.isPermaLocked() || isoWindow.Locked && owner.getCurrentSquare().Is(IsoFlagType.exterior)) {
                            owner.setVariable("OpenWindowOutcome", "struggle");
                        } else {
                            owner.setVariable("OpenWindowOutcome", "success");
                        }

                        return;
                    }

                    if (event.m_ParameterValue.equalsIgnoreCase(owner.getVariableString("StopAfterAnimLooped"))) {
                        owner.setVariable("bOpenWindow", false);
                    }
                }

                if (event.m_EventName.equalsIgnoreCase("WindowOpenAttempt")) {
                    this.onAttemptFinished(owner, isoWindow);
                } else if (event.m_EventName.equalsIgnoreCase("WindowOpenSuccess")) {
                    this.onSuccess(owner, isoWindow);
                } else if (event.m_EventName.equalsIgnoreCase("WindowStruggleSound") && "struggle".equals(owner.getVariableString("OpenWindowOutcome"))) {
                    owner.playSound("WindowIsLocked");
                }
            }
        }
    }

    @Override
    public boolean isDoingActionThatCanBeCancelled() {
        return true;
    }

    private void onAttemptFinished(IsoGameCharacter isoGameCharacter, IsoWindow isoWindow) {
        HashMap hashMap = isoGameCharacter.getStateMachineParams(this);
        this.exert(isoGameCharacter);
        if (isoWindow.isPermaLocked()) {
            if (!isoGameCharacter.getEmitter().isPlaying("WindowIsLocked")) {
            }

            isoGameCharacter.setVariable("OpenWindowOutcome", "fail");
            isoGameCharacter.setVariable("StopAfterAnimLooped", "fail");
        } else {
            byte _byte = 10;
            if (isoGameCharacter.Traits.Burglar.isSet()) {
                _byte = 5;
            }

            if (isoWindow.Locked && isoGameCharacter.getCurrentSquare().Is(IsoFlagType.exterior)) {
                if (Rand.Next(100) < _byte) {
                    isoGameCharacter.getEmitter().playSound("BreakLockOnWindow", isoWindow);
                    isoWindow.setPermaLocked(true);
                    isoWindow.syncIsoObject(false, (byte)0, null, null);
                    hashMap.put(PARAM_WINDOW, null);
                    isoGameCharacter.setVariable("OpenWindowOutcome", "fail");
                    isoGameCharacter.setVariable("StopAfterAnimLooped", "fail");
                    return;
                }

                boolean _boolean = false;
                if (isoGameCharacter.getPerkLevel(PerkFactory.Perks.Strength) > 7 && Rand.Next(100) < 20) {
                    _boolean = true;
                } else if (isoGameCharacter.getPerkLevel(PerkFactory.Perks.Strength) > 5 && Rand.Next(100) < 10) {
                    _boolean = true;
                } else if (isoGameCharacter.getPerkLevel(PerkFactory.Perks.Strength) > 3 && Rand.Next(100) < 6) {
                    _boolean = true;
                } else if (isoGameCharacter.getPerkLevel(PerkFactory.Perks.Strength) > 1 && Rand.Next(100) < 4) {
                    _boolean = true;
                } else if (Rand.Next(100) <= 1) {
                    _boolean = true;
                }

                if (_boolean) {
                    isoGameCharacter.setVariable("OpenWindowOutcome", "success");
                }
            } else {
                isoGameCharacter.setVariable("OpenWindowOutcome", "success");
            }
        }
    }

    private void onSuccess(IsoGameCharacter isoGameCharacter, IsoWindow isoWindow) {
        isoGameCharacter.setVariable("StopAfterAnimLooped", "success");
        IsoPlayer.getInstance().ContextPanic = 0.0F;
        if (isoWindow.getObjectIndex() != -1 && !isoWindow.open) {
            isoWindow.ToggleWindow(isoGameCharacter);
        }
    }

    private void exert(IsoGameCharacter isoGameCharacter) {
        float _float = zombie.GameTime.getInstance().getMultiplier() / 1.6F;
        switch (isoGameCharacter.getPerkLevel(PerkFactory.Perks.Fitness)) {
            case 1:
                isoGameCharacter.exert(0.01F * _float);
                break;
            case 2:
                isoGameCharacter.exert(0.009F * _float);
                break;
            case 3:
                isoGameCharacter.exert(0.008F * _float);
                break;
            case 4:
                isoGameCharacter.exert(0.007F * _float);
                break;
            case 5:
                isoGameCharacter.exert(0.006F * _float);
                break;
            case 6:
                isoGameCharacter.exert(0.005F * _float);
                break;
            case 7:
                isoGameCharacter.exert(0.004F * _float);
                break;
            case 8:
                isoGameCharacter.exert(0.003F * _float);
                break;
            case 9:
                isoGameCharacter.exert(0.0025F * _float);
                break;
            case 10:
                isoGameCharacter.exert(0.002F * _float);
        }
    }

    private void slideX(IsoGameCharacter isoGameCharacter, float float1) {
        float float0 = 0.05F * zombie.GameTime.getInstance().getMultiplier() / 1.6F;
        float0 = float1 > isoGameCharacter.x ? Math.min(float0, float1 - isoGameCharacter.x) : Math.max(-float0, float1 - isoGameCharacter.x);
        isoGameCharacter.x += float0;
        isoGameCharacter.nx = isoGameCharacter.x;
    }

    private void slideY(IsoGameCharacter isoGameCharacter, float float1) {
        float float0 = 0.05F * zombie.GameTime.getInstance().getMultiplier() / 1.6F;
        float0 = float1 > isoGameCharacter.y ? Math.min(float0, float1 - isoGameCharacter.y) : Math.max(-float0, float1 - isoGameCharacter.y);
        isoGameCharacter.y += float0;
        isoGameCharacter.ny = isoGameCharacter.y;
    }

    public void setParams(IsoGameCharacter owner, IsoWindow window) {
        HashMap hashMap = owner.getStateMachineParams(this);
        hashMap.clear();
        hashMap.put(PARAM_WINDOW, window);
    }
}
